package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.mapper.AddressBookMapper;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.List;

@Service
public class AddressBookServiceImpl implements AddressBookService {

    @Autowired
    private AddressBookMapper addressBookMapper;




    @Override
    public void add(AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBook.setIsDefault(0);
        addressBookMapper.insert(addressBook);
    }

    @Override
    public List<AddressBook> list() {
        Long userId = BaseContext.getCurrentId();
       List<AddressBook> addressBookList =  addressBookMapper.selectByUserId(userId);
       return addressBookList;
    }

    @Override
    public AddressBook getById(Long id) {

       AddressBook addressBook =  addressBookMapper.selectById(id);
       return addressBook;
    }

    @Override
    public void putById(AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookMapper.update(addressBook);
    }

    @Override
    public void removeById(Long id) {
        addressBookMapper.deleteById(id);
    }

    @Override
    public void setDefault(AddressBook addressBook) {
        //将用户所有的地址设置为非默认地址
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBook.setIsDefault(0);
        addressBookMapper.updateIsDefaultByUserId(addressBook);

        //在通过id设置默认ID
        addressBook.setIsDefault(1);
        addressBookMapper.updateIsDefaultById(addressBook);
    }

    @Override
    public List<AddressBook> get(AddressBook addressBook) {
    List<AddressBook>  addressBookList = addressBookMapper.selectByUserIdWithIsDefault(addressBook);
    return addressBookList;
    }


}
